package com.zyh.controller;

import com.zyh.entity.Book;
import com.zyh.entity.Cart;
import com.zyh.entity.CartItem;
import com.zyh.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ModelAndView mav;
    @Autowired
    private BookService bookService;

    @RequestMapping("/addCart")
    public ModelAndView addCart(Integer bookId, HttpSession session) {
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            session.setAttribute("cart", cart);
        }
        //把购买的图书放入购物车
        //判断购物车是否已经有该图书
        //查看buyMap是否有该图书Id
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        if(buyMap.containsKey(bookId)){
        //书已经存在，数量加1
            CartItem cartItem = buyMap.get(bookId);
            cartItem.setCount(cartItem.getCount()+1);
        }else{
            //书不存在
            CartItem cartItem = new CartItem();
            Book book = bookService.selectByBookId(bookId);
            cartItem.setBook(book);
            cartItem.setCount(1);
            buyMap.put(bookId,cartItem);
        }
        //计算总价
        Double totalPrice = 0.0;
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            //书的价格
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            //书的数量
            Integer countA = entry.getValue().getCount();
            //书的原价
            Double bookPrice = entry.getValue().getBook().getBookPrice();

            totalPrice += dangPrice * countA;
            //计算节省的价钱
            saveMoney += (bookPrice-dangPrice) * countA;
        }
        cart.setTotalMoney(totalPrice);
        cart.setSaveMoney(saveMoney);
        mav.setViewName("redirect:/cart/cart_list.jsp");
        return mav;
    }

    @RequestMapping("/removeCart")
    public ModelAndView removeCart(Integer bookId,HttpSession session){
        //获取购物车
        Cart cart = (Cart) session.getAttribute("cart");
        //购物车的购买集合中拿出要删除的商品项
        Map<Integer,CartItem> buyMap = cart.getBuyMap();
        CartItem cartItem = buyMap.get(bookId);
        buyMap.remove(bookId);

        Map<Integer,CartItem> removeMap = cart.getRemoveMap();
        removeMap.put(bookId,cartItem);

        //计算总价
        Double totalPrice = 0.0;
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            //书的价格
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            //书的数量
            Integer countR = entry.getValue().getCount();
            //书的原价
            Double bookPrice = entry.getValue().getBook().getBookPrice();

            totalPrice += dangPrice * countR;
            //计算节省的价钱
            saveMoney += (bookPrice-dangPrice) * countR;
        }
        cart.setTotalMoney(totalPrice);
        cart.setSaveMoney(saveMoney);
        mav.setViewName("/cart/cart_list.jsp");
        return mav;
    }

    @RequestMapping("updateCart")
    public ModelAndView updateCart(@Param("bookId") Integer bookId,@Param("countUp") Integer countUp, HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");
        //获取要变更的商品项
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        CartItem cartItem = buyMap.get(bookId);
        //将商品项的数量属性变更为count
        cartItem.setCount(countUp);

        //计算总价
        Double totalPrice = 0.0;
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            //书的价格
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            //书的数量
            Integer countNum = entry.getValue().getCount();
            //书的原价
            Double bookPrice = entry.getValue().getBook().getBookPrice();

            totalPrice += dangPrice * countNum;
            //计算节省的价钱
            saveMoney += (bookPrice-dangPrice) * countNum;
        }
        cart.setTotalMoney(totalPrice);
        cart.setSaveMoney(saveMoney);
        mav.setViewName("/cart/cart_list.jsp");
        return mav;
    }

    @RequestMapping("/resetCart")
    public ModelAndView resetCart(Integer bookId,HttpSession session){
        //获取购物车
        Cart cart = (Cart) session.getAttribute("cart");
        //购物车的删除集合中拿出要恢复的商品项
        Map<Integer,CartItem> removeMap = cart.getRemoveMap();
        CartItem cartItem = removeMap.get(bookId);
        removeMap.remove(bookId);

        Map<Integer,CartItem> buyMap = cart.getBuyMap();
        buyMap.put(bookId,cartItem);

        //计算总价
        Double totalPrice = 0.0;
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            //书的价格
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            //书的数量
            Integer countS = entry.getValue().getCount();
            //书的原价
            Double bookPrice = entry.getValue().getBook().getBookPrice();

            totalPrice += dangPrice * countS;
            //计算节省的价钱
            saveMoney += (bookPrice-dangPrice) * countS;
        }
        cart.setTotalMoney(totalPrice);
        cart.setSaveMoney(saveMoney);
        mav.setViewName("/cart/cart_list.jsp");
        return mav;
    }
}
